package restaraunt.rating.project;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class FoodRating {
	
	public static final String KEY_ROWID = "_id";
	public static final String KEY_FOODNAME = "food_name";
	public static final String KEY_FOODSCOR = "food_score";
	public static final String KEY_COMMENTS = "food_comments";
	public static final String KEY_RESTNAME = "restaraunt_name";
	
	private static final String DATABASE_NAME = "food_ratingdb";
	private static final String DATABASE_TABLE = "food_table";
	private static final int DATABASE_VERSION = 1;
	
	private DbHelper ourHelper;
	private final Context ourContext;
	private SQLiteDatabase ourDatabase;
	
	private static class DbHelper extends SQLiteOpenHelper {

		public DbHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
				KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
				KEY_FOODNAME + " TEXT NOT NULL, " +
				KEY_FOODSCOR + " TEXT NOT NULL, " +
				KEY_COMMENTS + " TEXT NOT NULL, " +
				KEY_RESTNAME + " TEXT NOT NULL);");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		}
	}
	
	public FoodRating(Context c) {
		ourContext = c;
	}
	
	public FoodRating open() throws SQLException {
		ourHelper = new DbHelper(ourContext);
		ourDatabase = ourHelper.getWritableDatabase();
		return this;
	}
	
	public void close() {
		ourHelper.close();
	}

	public long createEntry(String foodName, String foodRate, String foodComm,
			String restName) {
		ContentValues cv = new ContentValues();
		cv.put(KEY_FOODNAME, foodName);
		cv.put(KEY_FOODSCOR, foodRate);
		cv.put(KEY_COMMENTS, foodComm);
		cv.put(KEY_RESTNAME, restName);
		return ourDatabase.insert(DATABASE_TABLE, null, cv);
	}
	
	public String[][] getData()
	{
		/* I don't think it would be a good idea to return a string of all the
		 * data fields. Returning a two-dim array of separate data elements would
		 * make the presentation cleaner, but should only be implemented once the
		 * data retrieval concept has been proven.
		 */
		
		
		
		String[] columns = new String[]{KEY_ROWID, KEY_FOODNAME, KEY_FOODSCOR, KEY_COMMENTS, KEY_RESTNAME};
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
		
		List<String[]> dbResult = new ArrayList<String[]>();
		
		int iRow = c.getColumnIndex(KEY_ROWID);
		int iName = c.getColumnIndex(KEY_FOODNAME);
		int iScor = c.getColumnIndex(KEY_FOODSCOR);
		int iComm = c.getColumnIndex(KEY_COMMENTS);
		int iRest = c.getColumnIndex(KEY_RESTNAME);
		
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
		{
			dbResult.add(new String[] {
					c.getString(iRow),
					c.getString(iName),
					c.getString(iScor),
					c.getString(iComm),
					c.getString(iRest)
					}
				);
		}
		
		return (String[][])dbResult.toArray(new String[dbResult.size()][]);
	}
}
